package yxy.apple.util;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtillity {

	/***
	 * 判断字符串是否为空
	 * 
	 * @param string
	 * @return true OR false
	 */
	public static boolean isEmpty(String string) {
		int len = 0;
		if ((string == null) || ((len = string.length()) == 0)) {
			return true;
		}
		while (len-- > 0) {
			if (!Character.isWhitespace(string.charAt(len))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 将字符串source用prefix在前端补足长度到length
	 * 
	 * @param source
	 * @param prefix
	 * @param length
	 * @return
	 */
	public static String prefix(String source, char prefix, int length) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length - source.length(); i++) {
			buffer.append(prefix);
		}
		buffer.append(source);
		return buffer.toString();
	}

	/**
	 * 将字符串source用suffix在后端端补足长度到length
	 * 
	 * @param source
	 * @param suffix
	 * @param length
	 * @return
	 */
	public static String suffix(String source, char suffix, int length) {
		StringBuffer buffer = new StringBuffer(source);
		for (int i = 0; i < length - source.length(); i++) {
			buffer.append(suffix);
		}
		return buffer.toString();
	}

	public static String joint(String[] array, String split) {
		if (array != null) {
			if (array.length > 0) {
				StringBuffer buffer = new StringBuffer(array[0]);
				for (int i = 1; i < array.length; i++) {
					buffer.append(split);
					buffer.append(array[i]);
				}
				return buffer.toString();
			} else {
				return "";
			}
		} else {
			return null;
		}
	}

	/**
	 * 
	 * @param content
	 * @param regex
	 * @param operation operation java.util.regex.Pattern
	 * @param index
	 * @return
	 */
	public static String find(String content, String regex, int operation, int index) {
		if (content == null || regex == null) {
			return null;
		}
		Pattern pattern = Pattern.compile(regex, operation);
		Matcher matcher = pattern.matcher(content);
		if (matcher.find()) {
			return matcher.group(index);
		} else {
			return null;
		}
	}

	/**
	 * 
	 * @param content
	 * @param regex
	 * @param operation java.util.regex.Pattern
	 * @return
	 */
	public static String find(String content, String regex, int operation) {
		return find(content, regex, operation, 1);
	}

	public static String find(String content, String regex) {
		return find(content, regex, 0);
	}

	public static String replace(String text, char[] specimens, String[] replacements) {
		if (text != null && specimens != null && replacements != null && specimens.length == replacements.length) {
			StringBuffer buffer = new StringBuffer();
			char[] chars = text.toCharArray();
			for (int i = 0; i < chars.length; i++) {
				String replacement = null;
				char c = chars[i];
				for (int j = 0; j < specimens.length; j++) {
					if (c == specimens[j]) {
						replacement = replacements[j];
						break;
					}
				}
				if (replacement != null) {
					buffer.append(replacement);
				} else {
					buffer.append(c);
				}
			}
			return buffer.toString();
		} else {
			return text;
		}
	}

	public static String escapeHtml(String text) {
		return replace(text, new char[] { '&', '\"', '<', '>', ' ', '\n' }, new String[] { "&amp;", "&quot;", "&lt;", "&gt;", "&nbsp;", "<br />\n" });
	}

	public static byte[] toUTF8ByteArray(String string) {
		try {
			return string.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			return string.getBytes();
		}
	}

	/** 首字母大写 */
	public static String capitalize(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return str;
		}
		char firstChar = str.charAt(0);
		if (Character.isTitleCase(firstChar)) {
			return str;
		}
		return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
	}

	/**
	 * 转半角的函数(DBC case)<br/>
	 * <br/>
	 * 全角空格为12288，半角空格为32 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
	 * 
	 * @param input 任意字符串
	 * @return 半角字符串
	 *
	 */
	public static String toDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32; // 全角空格为12288，半角空格为32
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248); // 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
		}
		return new String(c);
	}

	/**
	 * 转全角的方法(SBC case)<br/>
	 * <br/>
	 * 全角空格为12288，半角空格为32 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
	 * 
	 * @param input 任意字符串
	 * @return 半角字符串
	 *
	 */
	public static String toSBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 32) {
				c[i] = (char) 12288;
				continue;
			}
			if (c[i] > 32 && c[i] < 127)
				c[i] = (char) (c[i] + 65248);
		}
		return new String(c);
	}

	/** 统计字符串source中有多少个str */
	public static int count(String source, String str) {
		int count = 0;
		int index = source.indexOf(str, 0);
		while (index >= 0) {
			count++;
			index = source.indexOf(str, index+str.length());
		}
		return count;
	}

}
